<template>
  <div>
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="原始id"
            prop="id"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.id" filterable clearable>
              <el-option
                v-for="(data,index) in columns"
                :key="index"
                :label="data.label"
                :value="data.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="部门名称"
            prop="name"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.name" filterable clearable>
              <el-option
                v-for="(data,index) in columns"
                :key="index"
                :label="data.label"
                :value="data.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="部门编码"
            prop="code"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.code" filterable clearable>
              <el-option
                v-for="(data,index) in columns"
                :key="index"
                :label="data.label"
                :value="data.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="父节点"
            prop="pid"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-select v-model="form.pid" filterable clearable>
              <el-option
                v-for="(data,index) in columns"
                :key="index"
                :label="data.label"
                :value="data.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="custom-button" style="text-align: right;padding: 0px 60px;box-sizing: border-box">
      <el-button @click="close('form')">取消</el-button>
      <el-button type="primary" :loading="loading" @click="submitForm('form')">保存</el-button>
    </div>
  </div>
</template>
<script>
const defaultForm = { name: '', code: '', pid: '', id: '' }
import { getEntity } from '@/api/quartz/data/index'
import { updateObject } from '@/api/quartz/task/index'
export default {
  name: 'TaskSettingDepartment',
  props: {
    template: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: defaultForm,
      columns: [],
      loading: false
    }
  },
  watch: {
    template: {
      handler(newData, oldData) {
        if (undefined !== newData) {
          if (undefined !== newData.queryType && newData.queryType !== null) { // 根据数据来源，查询数据解析信息
            if (newData.queryType === 'sql') { // 来自于数据库查询
              this.initColumns()
            } else if (newData.queryType === 'api') { // 来源于接口数据

            }
          }
          if (undefined !== newData.jxx && newData.jxx !== null) {
            this.form = JSON.parse(newData.jxx)
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    initColumns() {
      getEntity({ id: this.template.dataId }).then(res => {
        const { data } = res
        if (undefined !== data && data !== null && undefined !== data.data.pzx && data.data.pzx !== '') {
          const newData = JSON.parse(data.data.pzx).map(item => {
            const backValue = { label: item, value: item }
            return backValue
          })
          this.columns = newData
        } else {
          this.columns = []
        }
      }).catch(() => {
        this.columns = []
      })
    },
    submitForm(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.template.jxx = JSON.stringify(this.form)
          updateObject(this.template).then(res => {
            this.$message.success('保存成功！')
            this.loading = false
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
          return false
        }
      })
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = defaultForm
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>

</style>
